package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.sgb.kjwl.model.entity.PowerRequestEntity;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * 建设力量筛选弹窗（根据PC端需求，均为单选）
 * typeNo：资质类型(默认给"")
 * levelNo：资质等级(默认给"")（选择typeNo后的二级筛选）
 * recordNo：备案单位(默认给"")
 * areaNo：区域(1省内0省外，默认1)
 */
public class PowerFliterPopWindow extends BasePopWindow {
    private TextView areaView;
    private TextView typeView;
    private TextView levelView;
    private TextView recordView;
    private TextView okView;
    private TextView resetView;

    private MaxRecyclerView areaRecyclerList;
    private MaxRecyclerView levelList;
    private MaxRecyclerView typeList;
    private MaxRecyclerView recordList;

    private MaterialFilterAdapter typeAdapter;
    private MaterialFilterAdapter levelAdapter;
    private MaterialFilterAdapter recordAdapter;
    private MaterialFilterAdapter areaAdapter;

    private FilterResultClick filterResultClick;

    private PowerRequestEntity filterJson;

    private int typePosition = -1;

    List<MaterialFilterEntity> firstSecondDatas;


    private int position1 = -1;
    private int position2 = -1;
    private int position3 = -1;
    private int position4 = -1;

    private String name1 = "";
    private String name2 = "";
    private String name3 = "";
    private String name4 = "";


    private String no1 = "";
    private String no2 = "";
    private String no3 = "";
    private String no4 = "";


    public interface FilterResultClick {
        void result(PowerRequestEntity filterJson);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public PowerFliterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_power_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);

        typeView = holder.getPop().findViewById(R.id.type_view);
        levelView = holder.getPop().findViewById(R.id.level_view);
        recordView = holder.getPop().findViewById(R.id.record_view);
        areaView = holder.getPop().findViewById(R.id.area_view);

        typeList = holder.getPop().findViewById(R.id.type_list);
        levelList = holder.getPop().findViewById(R.id.level_list);
        recordList = holder.getPop().findViewById(R.id.record_list);
        areaRecyclerList = holder.getPop().findViewById(R.id.area_list);

        typeList.setLayoutManager(new GridLayoutManager(mContext, 3));
        levelList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        areaRecyclerList.setLayoutManager(new GridLayoutManager(mContext, 3));

        //资质类型
        typeAdapter = new MaterialFilterAdapter(mContext, null);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {

            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                if (typePosition != position) {
                    typePosition = position;
                    filterJson.setTypeNo(materialFilterEntity.getId());
                    levelView.setVisibility(View.VISIBLE);
                    levelList.setVisibility(View.VISIBLE);
                    levelAdapter.setDatas(materialFilterEntity.getDatas());
                    levelAdapter.setDefItem(-1);
                    filterJson.setLevelNo("");

                    recordAdapter.setDefItem(-1);
                    filterJson.setRecordNo("");

                    position1 = position;
                    no1 = materialFilterEntity.getId();
                    name1 = materialFilterEntity.getName();

                    position2 = -1;
                    no2 = "";
                    name2 = "";

                    position3 = -1;
                    no3 = "";
                    name3 = "";
                }
            }
        });
        typeList.setAdapter(typeAdapter);
        //资质等级
        levelAdapter = new MaterialFilterAdapter(mContext, null);
        levelAdapter.setItemSelect(true);
        levelAdapter.setDefItem(-1);
        levelAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                filterJson.setLevelNo(materialFilterEntity.getId());

                recordAdapter.setDefItem(-1);
                filterJson.setRecordNo("");

                position2 = position;
                no2 = materialFilterEntity.getId();
                name2 = materialFilterEntity.getName();

                position3 = -1;
                no3 = "";
                name3 = "";
            }
        });
        levelList.setAdapter(levelAdapter);

        //备案单位
        recordAdapter = new MaterialFilterAdapter(mContext, null);
        recordAdapter.setItemSelect(true);
        recordAdapter.setDefItem(-1);
        recordAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                filterJson.setRecordNo(materialFilterEntity.getId());
                position3 = position;
                no3 = materialFilterEntity.getId();
                name3 = materialFilterEntity.getName();

            }
        });
        recordList.setAdapter(recordAdapter);

        //区域，省内、省外
        areaAdapter = new MaterialFilterAdapter(mContext, null);
        areaAdapter.setItemSelect(true);
        areaAdapter.setDefItem(0);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                filterJson.setAreaNo(materialFilterEntity.getId());
            }
        });
        areaRecyclerList.setAdapter(areaAdapter);

        filterJson = new PowerRequestEntity();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (filterResultClick != null) {
                    filterResultClick.result(filterJson);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {

        typeAdapter.setDefItem(-1);
        typePosition = -1;
        filterJson.setTypeNo("");

        levelView.setVisibility(View.GONE);
        levelList.setVisibility(View.GONE);
        levelAdapter.setDefItem(-1);
        filterJson.setLevelNo("");

        recordAdapter.setDefItem(-1);
        filterJson.setRecordNo("");

        areaAdapter.setDefItem(0);
        filterJson.setAreaNo("1");
        position1 = -1;
        position2 = -1;
        position3 = -1;
        position4 = -1;

        name1 = "";
        name2 = "";
        name3 = "";
        name4 = "";


        no1 = "";
        no2 = "";
        no3 = "";
        no4 = "";
        dissMiss();
        if (filterResultClick != null) {
            filterResultClick.result(filterJson);
        }

    }

    public void setTypeLevelData(List<MaterialFilterEntity> data) {
        if (data.size() == 0) {
            typeView.setVisibility(View.GONE);
        } else {
            typeView.setVisibility(View.VISIBLE);
        }
        typeAdapter.setDatas(data);
        firstSecondDatas = data;
    }

    public void setRecordData(List<MaterialFilterEntity> data) {
        if (data.size() == 0) {
            recordView.setVisibility(View.GONE);
        } else {
            recordView.setVisibility(View.VISIBLE);
        }
        recordAdapter.setDatas(data);
    }

    public void setAreaData(List<MaterialFilterEntity> data) {
        /*if (data.size() == 0) {
            areaView.setVisibility(View.GONE);
        } else {
            areaView.setVisibility(View.VISIBLE);
        }*/
        areaRecyclerList.setVisibility(View.GONE);
        areaAdapter.setDatas(data);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public void setDefData1(int position, String no,String name) {
        typeAdapter.setDefItem(position);
        filterJson.setTypeNo(no);

        levelView.setVisibility(View.VISIBLE);
        levelList.setVisibility(View.VISIBLE);
        levelAdapter.setDatas(firstSecondDatas.get(position).getDatas());

        levelAdapter.setDefItem(-1);
        filterJson.setLevelNo("");

        recordAdapter.setDefItem(-1);
        filterJson.setRecordNo("");

        no1 = "".equals(no) ? "" : no;
        name1 = "全部".equals(name) ? "" : name;
        position1 = position == 0 ? -1 : position;
        filterJson.setTypeNo(no);

        no2 = "";
        name2 = "";
        position2 = -1;
        filterJson.setLevelNo("");

        no3 = "";
        name3 = "";
        position3 = -1;
        filterJson.setRecordNo("");


    }


    public void setDefData2(int position, String no,String name) {


        levelAdapter.setDefItem(position);
        filterJson.setLevelNo(no);

        recordAdapter.setDefItem(-1);
        filterJson.setRecordNo("");

        no2 = "".equals(no) ? "" : no;
        name2 = "全部".equals(name) ? "" : name;
        position2 = position == 0 ? -1 : position;
        filterJson.setLevelNo(no);


        no3 = "";
        name3 = "";
        position3 = -1;
        filterJson.setRecordNo("");
    }

    public void setDefData3(int position, String no,String name) {


        recordAdapter.setDefItem(position);
        filterJson.setRecordNo(no);

        no3 = "".equals(no) ? "" : no;
        name3 = "全部".equals(name) ? "" : name;
        position3 = position == 0 ? -1 : position;
        filterJson.setRecordNo(no);



    }

    public int getPosition1() {
        return position1;
    }

    public int getPosition2() {
        return position2;
    }

    public int getPosition3() {
        return position3;
    }

    public int getPosition4() {
        return position4;
    }

    public String getName1() {
        return name1;
    }

    public String getName2() {
        return name2;
    }

    public String getName3() {
        return name3;
    }

    public String getName4() {
        return name4;
    }

    public String getNo1() {
        return no1;
    }

    public String getNo2() {
        return no2;
    }

    public String getNo3() {
        return no3;
    }

    public String getNo4() {
        return no4;
    }
}
